package com.example.config.security.authentication;
import com.example.pojo.Emp;
import com.example.pojo.Role;
import com.example.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 自定义Security账户登录验证类
 * @author wsl
 */
@Service("userDetailsService")//替换Security原生的验证类
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IEmpService empService;

    /**
     * 根据前端用户传递的用户名查询用户
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Emp emp = empService.getEmpByUserName(username);
        if(emp != null){
            List<Role> roles = empService.getRolesByEmpId(emp.getId());
            emp.setRoles(roles);
        }
        return emp;
    }
}
